package com.ruoyi.kanban.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kanban.domain.SupplyFramework;
import com.ruoyi.kanban.service.ISupplyFrameworkService;

@RestController
@RequestMapping("/kanban/supplyFramework")
public class SupplyFrameworkController extends BaseController {

    @Autowired
    private ISupplyFrameworkService supplyFrameworkService;

    @PreAuthorize("@ss.hasPermi('kanban:framework:list')")
    @GetMapping("/list")
    public TableDataInfo list(SupplyFramework query,
                             @RequestParam(value = "beginBusinessDate", required = false) String beginBusinessDate,
                             @RequestParam(value = "endBusinessDate", required = false) String endBusinessDate) {
        startPage();

        // 将日期参数设置到params中
        if (beginBusinessDate != null || endBusinessDate != null) {
            if (query.getParams() == null) {
                query.setParams(new java.util.HashMap<>());
            }
            if (beginBusinessDate != null) {
                query.getParams().put("beginBusinessDate", beginBusinessDate);
            }
            if (endBusinessDate != null) {
                query.getParams().put("endBusinessDate", endBusinessDate);
            }
        }

        List<SupplyFramework> list = supplyFrameworkService.selectSupplyFrameworkList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('kanban:framework:list')")
    @GetMapping("/listValid")
    public TableDataInfo listValid(SupplyFramework query,
                                  @RequestParam(value = "beginBusinessDate", required = false) String beginBusinessDate,
                                  @RequestParam(value = "endBusinessDate", required = false) String endBusinessDate) {
        startPage();

        // 将日期参数设置到params中
        if (query.getParams() == null) {
            query.setParams(new java.util.HashMap<>());
        }
        if (beginBusinessDate != null) {
            query.getParams().put("beginBusinessDate", beginBusinessDate);
        }
        if (endBusinessDate != null) {
            query.getParams().put("endBusinessDate", endBusinessDate);
        }

        List<SupplyFramework> list = supplyFrameworkService.selectValidSupplyFrameworkList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('kanban:framework:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(supplyFrameworkService.selectSupplyFrameworkById(id));
    }

    @PreAuthorize("@ss.hasPermi('kanban:framework:add')")
    @Log(title = "供货框架", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SupplyFramework supplyFramework) {
        return toAjax(supplyFrameworkService.insertSupplyFramework(supplyFramework));
    }

    @PreAuthorize("@ss.hasPermi('kanban:framework:edit')")
    @Log(title = "供货框架", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SupplyFramework supplyFramework) {
        return toAjax(supplyFrameworkService.updateSupplyFramework(supplyFramework));
    }

    @PreAuthorize("@ss.hasPermi('kanban:framework:remove')")
    @Log(title = "供货框架", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(supplyFrameworkService.deleteSupplyFrameworkByIds(Arrays.stream(ids).collect(Collectors.toList())));
    }
}
